package make.free.app.mcaro.game;

import make.free.app.mcaro.R;
import make.free.app.mcaro.activity.Game;
import make.free.app.mcaro.bluetooth.BluetoothChatService;
import make.free.app.mcaro.util.Constan;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class ProcessBar {
	Rectangle mRectangle;
	private float width_rect = 0;

	private BitmapTextureAtlas prb_BTA;
	private Sprite prb_SP;
	private TextureRegion prb_TR;
	private Context mContext;
	private Engine mEngine;
	private Scene mScene;
	private int position;
	/**
	 * Tổng th�?i gian của level
	 */
	private int total_time = Constan.TIME_CHANGE;

	/**
	 * Th�?i gian hiện tại đang chạy
	 */
	private int current_time = 0;

	/**
	 * Tạm dừng hoặc tiếp tục
	 */
	private boolean isPause = false;
	private boolean isStop = false;
	private BluetoothChatService mChatService = null;

	// Lưu lại các giá trị

	// -----------------------------------------------

	public ProcessBar(Context mContext, Engine mEngine, Scene mScene,
			int postition,BluetoothChatService mChatServices) {
		this.mContext = mContext;
		this.mScene = mScene;
		this.mEngine = mEngine;
		this.position = postition;
		this.mChatService = mChatServices;
	}

	// -----------------------------------------------
	public void onLoadResources() {
		this.prb_BTA = new BitmapTextureAtlas(512, 64,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.prb_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.prb_BTA, mContext, "progressbar.png", 0, 0);
		this.mEngine.getTextureManager().loadTextures(this.prb_BTA);
	}

	// -----------------------------------------------
	public void onLoadScene() {
		// vi tri x
		int x = position - 100;

		// lưu lại chi�?u rộng
		width_rect = 193;

		this.mRectangle = new Rectangle(x + 3, 5, 215, 65);
		this.mRectangle.setColor(0f, 0.98f, 0.65f);
		this.mScene.attachChild(mRectangle);

		this.prb_SP = new Sprite(x, 2, 200, 74, this.prb_TR);
		this.mScene.attachChild(prb_SP);
	}

	// -----------------------------------------------
	float w = -1;

	/**
	 * Bắt đầu chạy th�?i gian đếm ngược
	 */
	public void start() {
		if (total_time < 0)
			return;

		current_time = total_time + 1;
		Log.e("CuRent Time", "" + current_time);
		// Tính xem sao 1s thì cần phải thu nh�? cái hình chữ nhật 1 khoảng bao
		// nhiêu
		w = width_rect / current_time;
		mRectangle.setVisible(true);
		mRectangle.setWidth(width_rect);
		isStop = false;
		isPause = false;
		setTimer();

	}

	// -----------------------------------------------
	/**
	 * Thay đổi kích thước hình chữ nhật
	 * 
	 * @param w
	 */
	public void updateProgressBar(float w) {
		if (current_time > 0 && !this.isStop) {
			mRectangle.setWidth(mRectangle.getWidth() - w);
		} else {
			mRectangle.setVisible(false);
		}
	}

	// -----------------------------------------------

	public void setPause(boolean isPause) {
		this.isPause = isPause;
	}

	public void setStop(boolean isStop) {
		this.isStop = isStop;
		current_time = total_time;
	}

	// -----------------------------------------------

	public void setTimer() {

		Thread th = new Thread(new Runnable() {
			@Override
			public void run() {
				while (!isStop && current_time > 0) {
					if (!isPause) {
						try {
							Thread.sleep(1000);
							current_time--;
							// Log.e("CuRent Time",""+ current_time);
							updateProgressBar(w);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				}

				if (current_time <= 0) {
					if (Game.isGameAI) {
						EventTitle.Win = true;
						if (EventTitle.player_change == 2)
							Game.playerwin = "Bạn";
						else {

							if (Game.isGameAI)
								Game.playerwin = "Máy";
							else
								Game.playerwin = "Đối thủ";

						}
					}
					else
					{
						EventTitle.Win =true;
						Game.playerwin="lose";
						sendMessage("5-win");
						
					}
				}
				return;
			}

		});
		th.start();

	}
	private void sendMessage(String message) {
		// Check that we're actually connected before trying anything
		if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
			Toast.makeText(mContext, R.string.not_connected, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (message.length() > 0) {
			// Get the message bytes and tell the BluetoothChatService to write
			byte[] send = message.getBytes();
			mChatService.write(send);

		}
	}

}
